Public Class adm_searchauditresults
    Inherits System.Web.UI.Page
    Public strValidation As String = ""
    Public strHtml As String = ""
    Public oAudit As New gaAudit.SearchAudit
    Public aud_fd, aud_td As String
    Public aud_ft, aud_tt_copy As String
    Public bValidDates As Boolean
    Public objvalid As New gaValidation
    Public aUserSession(13) As String
    Public sess_cid, aff_cid, sess_gid, aff_gid As String
    Public sess_uid, aff_uid As String
    'Public aud_fd, aud_td As String
    Public aud_et As String
    Public aud_sip As String
    Public aud_rpp As String
    Public aud_do As String
    Public aud_rc As String
    Public aud_tt As String
    Public Const cSESSION_ID = 0
    Public Const cCOMPANY_ID = 1
    Public Const cGAPORTAL_ID = 2
    Public Const cUSER_ID = 3
    Public Const cINCIDCONTEXT = 4
    Public Const cINGIDCONTEXT = 5
    Public Const cLOGINDATETIME = 6
    Public Const cLASTACTIVITY = 7
    Public Const cUSERPERMISSIONSSTRING = 8
    Public Const cROLE_ID = 9
    Public Const cMEMBER_TYPE = 10
    Public Const cTERMINAL_NUMBER = 11
    Public Const cTERMINAL_MARKET = 12
    Protected WithEvents et As System.Web.UI.WebControls.DropDownList
    Protected WithEvents rpp As System.Web.UI.WebControls.DropDownList
    Protected WithEvents dofirst As System.Web.UI.WebControls.DropDownList
    Protected WithEvents submit As System.Web.UI.WebControls.Button
    Public sQS As String
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getparams()
        DispValidation()
        BuildEventype()
        buildRPP()
        builtdofirst()
        Display_Results()
    End Sub
    Private Sub getparams()
        If Not IsNothing(Request.QueryString("sess_gid")) Then
            If aUserSession(cCOMPANY_ID) = "0" Then
                sess_cid = Trim(Request.QueryString("sess_cid"))
                aff_cid = Trim(Request.QueryString("aff_cid"))
            Else
                sess_cid = Application("CompanyID")
                aff_cid = Application("CompanyID")
            End If
            sess_gid = Trim(Request.QueryString("sess_gid"))
            aff_gid = Trim(Request.QueryString("aff_gid"))
            sess_uid = Trim(Request.QueryString("sess_uid"))
            aff_uid = Trim(Request.QueryString("aff_uid"))
            aud_fd = Trim(Request.QueryString("fd"))
            'aud_ft = Trim(Request.QueryString("ft"))
            aud_td = Trim(Request.QueryString("td"))
            'aud_tt = Trim(Request.QueryString("tt"))
            aud_et = Trim(Request.QueryString("et"))
            aud_sip = Trim(Request.QueryString("sip"))
            aud_rpp = CInt(Request.QueryString("rpp"))
            aud_do = Trim(Request.QueryString("do"))
            aud_rc = CInt(Request.QueryString("rc"))

            sQS = ""
            sQS = sQS & "?sess_gid=" & Server.UrlEncode(sess_gid)
            sQS = sQS & "&sess_cid=" & Server.UrlEncode(sess_cid)
            sQS = sQS & "&sess_uid=" & Server.UrlEncode(sess_uid)
            sQS = sQS & "&aff_gid=" & Server.UrlEncode(aff_gid)
            sQS = sQS & "&aff_cid=" & Server.UrlEncode(aff_cid)
            sQS = sQS & "&aff_uid=" & Server.UrlEncode(aff_uid)
            sQS = sQS & "&fd=" & Server.UrlEncode(aud_fd)
            'sQS = sQS & "&ft=" & Server.UrlEncode(aud_ft)
            sQS = sQS & "&td=" & Server.UrlEncode(aud_td)
            'sQS = sQS & "&tt=" & Server.UrlEncode(aud_tt)
            sQS = sQS & "&et=" & Server.UrlEncode(aud_et)
            sQS = sQS & "&sip=" & Server.UrlEncode(aud_sip)
            sQS = sQS & "&rpp=" & Server.UrlEncode(aud_rpp)
            sQS = sQS & "&do=" & Server.UrlEncode(aud_do)
        Else
            Response.Redirect("adm_searchaudit.asp")

        End If
    End Sub
    Private Sub DispValidation()
        If Not oAudit.ValidateDate(CStr(aud_fd)) Or Not oAudit.ValidateDate(CStr(aud_td)) Then
            strValidation = "<div class=""error"">Please check that the dates entered are valid and in MM/DD/YY format</div><br>"

            bValidDates = False
        ElseIf aud_fd <> vbNullString And aud_td <> vbNullString Then
            If CDate(aud_fd & " " & aud_ft) > CDate(aud_td & " " & aud_tt_copy) Then

                strValidation += "<div class=""error"">Please be sure that the ""From Date/Time"" is prior to the ""To Date/Time"" entered</div><br>"
                bValidDates = False
            Else
                bValidDates = True
            End If
        ElseIf aud_fd <> vbNullString And aud_td = vbNullString Then
            If CDate(aud_fd & " " & aud_ft) > Now() Then

                strValidation += "<div class=""error"">Please be sure that the ""From Date/Time"" is prior to the current date/time</div><br>"

                bValidDates = False
            Else
                bValidDates = True
            End If
        ElseIf aud_fd = vbNullString And aud_td <> vbNullString Then
            If Now() > CDate(aud_td & " " & aud_tt_copy) Then

                strValidation += "<div class=""error"">Please be sure that the ""To Date/Time"" is after the current date/time</div><br>"

                bValidDates = False
            Else
                bValidDates = True
            End If
        Else
            bValidDates = True
        End If
    End Sub
    Private Sub BuildEventype()
        Dim oAudit As New gaAudit.SearchAudit
        Dim rst As New DataSet
        Dim i As Integer
        et.Items.Clear()
        et.Items.Add(New ListItem("All Event", 0))
        rst = oAudit.GetEvents()
        If rst.Tables(0).Rows.Count > 0 Then
            For i = 0 To rst.Tables(0).Rows.Count - 1
                'et.Items.Add(New ListItem(rst.Tables(0).Rows(i).Item("EventDescription"), rst.Tables(0).Rows(i).Item("Event_ID")))
                If CInt(rst.Tables(0).Rows(i).Item("Event_ID")) = CInt(aud_et) Then
                    et.Items.Add(New ListItem(rst.Tables(0).Rows(i).Item("EventDescription"), rst.Tables(0).Rows(i).Item("Event_ID")))
                    et.SelectedItem.Value = CInt(aud_et)
                Else
                    et.Items.Add(New ListItem(rst.Tables(0).Rows(i).Item("EventDescription"), rst.Tables(0).Rows(i).Item("Event_ID")))
                End If
            Next
        End If

    End Sub
    Private Sub buildRPP()
        Dim i As Integer
        Dim aRPP(5) As String
        rpp.Items.Clear()
        aRPP(0) = "10"
        aRPP(1) = "20"
        aRPP(2) = "30"
        aRPP(3) = "40"
        aRPP(4) = "50"
        For i = 0 To UBound(aRPP) - 1
            If aud_rpp = CInt(aRPP(i)) Then
                rpp.Items.Add(New ListItem(aRPP(i), i))
                rpp.SelectedItem.Text = CInt(aRPP(i))
            Else
                rpp.Items.Add(New ListItem(aRPP(i), i))
            End If
        Next
    End Sub
    Private Sub builtdofirst()
        Dim i As Integer
        Dim aDo(2) As String
        dofirst.Items.Clear()
        aDo(0) = "Newest First"
        aDo(1) = "Oldest First"
        For i = 0 To UBound(aDo) - 1
            dofirst.Items.Add(New ListItem(aDo(i), i))
            If aud_do = aDo(i) Then
                dofirst.SelectedItem.Text = aDo(i)
            Else
                dofirst.Items.Add(New ListItem(aDo(i), i))
            End If
        Next

    End Sub

    Public Sub Display_results()
        Dim bgcolor As String
        Dim nEndInd As Integer
        Dim nBegInd As Integer
        Dim aud_rc As Integer
        Dim aud_rpp As Integer
        Dim rstCursor As Integer
        Dim rst As DataSet
        Dim i As Integer
        Dim auAID As String
        Dim auET As String
        Dim auDT As String
        Dim auSESS_UID As String
        Dim auSESS_CID As String
        Dim auSESS_GID As String
        Dim auAFF_UID As String
        Dim auAFF_CID As String
        Dim auAFF_GID As String
        Dim auDATA As String
        Dim nTotalRecords As Integer
        strHtml = ""
        If bValidDates Then
            rst = oAudit.GetAuditRecords(Trim(sess_gid), Trim(sess_cid), Trim(sess_uid), Trim(aff_gid), Trim(aff_cid), Trim(aff_uid), Trim(aud_et), Trim(aud_sip), Trim(aud_fd), Trim(aud_ft), Trim(aud_td), Trim(aud_tt), Trim(aud_do))
            If rst.Tables(0).Rows.Count > 0 Then
                strHtml += "Select a link in the Date/Time column to view more detailed information on a specific event.<p>"
                strHtml += "<table border=0 cellpadding=1 cellspacing=3><tr><td></td><td>"
                strHtml += "DATE-TIME </td><td>"
                strHtml += "Event TYPE </td><td>"
                strHtml += "TRIGGERED BY </td>"
                If aUserSession(cCOMPANY_ID) = "0" Then
                    strHtml += "<td>System(ID)</td>"
                End If
                strHtml += "<td>User(ID)</td><td>"
                ''<!--%=application("AppName")%-->ACCOUNT ID

                strHtml += "</td><td>SOURCE(IP)</td></tr>"

                bgcolor = "#ffffff"
                nEndInd = aud_rc + aud_rpp 'Ending index of recordset.
                nBegInd = aud_rc 'Beginning index of recordset.
                rstCursor = 1
                '-----	incrementing recordset to aud_rc
                If rstCursor < aud_rc Then
                    rstCursor = rstCursor + rst.Tables(0).Rows.Count

                End If
                For i = 0 To rst.Tables(0).Rows.Count - 1
                    If aud_rc < nEndInd Then

                    End If

                    'Do While Not rst.eof And aud_rc < nEndInd
                    If bgcolor = "ffffff" Then
                        bgcolor = "dddddd"
                    Else
                        bgcolor = "ffffff"
                    End If
                    auAID = rst.Tables(0).Rows(i).Item("Audit_ID")
                    auET = rst.Tables(0).Rows(i).Item("EventDescription")
                    auDT = rst.Tables(0).Rows(i).Item("DateTime")
                    auSESS_UID = rst.Tables(0).Rows(i).Item("SESSION_User_ID")
                    auSESS_CID = rst.Tables(0).Rows(i).Item("SESSION_Company_ID")
                    auSESS_GID = rst.Tables(0).Rows(i).Item("SESSION_Viaklix_ID")
                    auAFF_UID = rst.Tables(0).Rows(i).Item("AFFECTING_User_ID")
                    auAFF_CID = rst.Tables(0).Rows(i).Item("AFFECTING_Company_ID")
                    auAFF_GID = rst.Tables(0).Rows(i).Item("AFFECTING_GAPORTAL_ID")
                    auDATA = rst.Tables(0).Rows(i).Item("Data")
                    strHtml += "<tr><td>" & aud_rc & "</td><td bgcolor=" & bgcolor & ">"

                    strHtml += "<a href='adm_searchauditdetails.aspx " & sQS & "&rc=" & nBegInd & " &aid=" & auAID & "'>" & auDT & "</a></td><td bgcolor=" & bgcolor & "><b>" & auET & "</b>"

                    strHtml += "</td><td bgcolor=" & bgcolor & "><b>" & auET & " ></b></td><td bgcolor=" & bgcolor & ">"
                    If aUserSession(cCOMPANY_ID) = "0" Then
                        strHtml += " & auSESS_CID & :: " & auSESS_GID & ":: " & auSESS_UID & ""
                    Else
                        strHtml += " &  auSESS_GID & :: " & auSESS_UID & ""

                    End If
                    If aUserSession(cCOMPANY_ID) = "0" Then
                        strHtml += "</td><td bgcolor=" & bgcolor & "><" & auAFF_CID & "</td>"

                    End If
                    strHtml += "<td bgcolor=" & bgcolor & ">" & auAFF_UID & "</td><td bgcolor=" & bgcolor & ">" & auAFF_GID & "</td><td bgcolor=" & bgcolor & ">"

                    If Len(Trim(auDATA)) > 0 And Not IsNothing(auDATA) Then
                        strHtml += " & auDATA & "
                    Else
                        strHtml += "---"
                    End If

                    strHtml += "</td></tr>"
                    aud_rc = aud_rc + 1

                Next
                nTotalRecords = aud_rc - 1
                nTotalRecords = nTotalRecords + rst.Tables(0).Rows.Count
                'If Not rst.eof Then
                '    Do While Not rst.eof
                '        nTotalRecords = nTotalRecords + 1
                '        rst.movenext()
                '    Loop
                'End If

                strHtml += "<tr><td colspan=7 align=""middle"">"
                strHtml += "Displaying Records " & nBegInd & " to " & aud_rc - 1 & " of " & nTotalRecords & "."

                strHtml += "<table border=0 cellpadding=1 cellspacing=1><tr>"

                If aud_rc - aud_rpp > 1 Then
                    strHtml += "<td><a href='adm_searchauditresults.aspx " & sQS & " &rc=" & nBegInd - aud_rpp & "'>Previous Page</a></td>"

                End If
                If aud_rc - 1 < nTotalRecords Then
                    strHtml += "<td><a href='adm_searchauditresults.aspx " & sQS & " &rc=" & aud_rc & "'> >Next Page </a></td>"

                End If
                strHtml += "</tr></table></td></tr></table>"
            Else
                strHtml += "NONE FOUND."
            End If

        End If
    End Sub

    Private Sub submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit.Click
        Response.Redirect("adm_searchauditresults.aspx")
    End Sub
End Class
